package com.zjgeport.zjgdanger;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

import com.soar.encrypt.SoarMd5;
import com.soar.util.StringUtils;
import com.soar.util.net.rmi.httpxml.HttpCallBack;
import com.soar.util.net.rmi.httpxml.HttpClient;
import com.soar.util.net.rmi.httpxml.HttpMethod;
import com.soar.util.net.rmi.httpxml.HttpResult;
import com.zjgeport.zjgdanger.common.ParamCommon;
import com.zjgeport.zjgdanger.model.UserInfo;
import com.zjgeport.zjgdanger.model.myApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LoginActivity extends CommonActivity {
    private EditText loginUserText;

    private Button loginButton;

    private ArrayAdapter<String> mAdapter;


    public List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();

    private SharedPreferences pref;

    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView()
    {
        pref = getSharedPreferences(ParamCommon.APP_CACHE_FILE_NAME, Activity.MODE_PRIVATE);
        // 判断是否自动登录
        String isAuto = pref.getString(ParamCommon.APP_IS_AUTO_LOGIN, "");
       if ("1".equals(isAuto)) {
           new Handler().postDelayed(new Runnable() {
               public void run() {
                   final String loginname = pref.getString(ParamCommon.APP_LOGIN_NAME, "");
                   final String password = pref.getString(ParamCommon.APP_LOGIN_PASSWORD, "");
                   if(loginname.length()>0 && password.length() >0)
                   {
                       login(loginname, password);
                   }

               }
           }, 0);
       }


        editor = pref.edit();
        findEditTextById(R.id.login_user).setText(pref.getString(ParamCommon.APP_LOGIN_NAME, ""));
        findEditTextById(R.id.login_pass).setText(pref.getString(ParamCommon.APP_LOGIN_PASSWORD, ""));
        this.loginButton = findButtonById(R.id.login);
        loginButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String loginname = LoginActivity.this.findEditTextStringById(R.id.login_user);
                String password = LoginActivity.this.findEditTextStringById(R.id.login_pass);
                if (StringUtils.isEmpty(loginname) || StringUtils.isEmpty(password))
                {
                    LoginActivity.this.showToast("账号、密码不能为空！");
                }
                else
                {

                    login(loginname, password);
                }
            }
        });
    }

    private void login(final String loginname, final String password)
    {
        // 进服务端验证登录用户名与密码及拉取菜单com2/88888888
        String pass = new SoarMd5().getMd5(password);
//        String _pass = pass.toUpperCase();
        String _pass = pass.toLowerCase();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("USERNAME", loginname);
        map.put("PASSWORD", _pass);
        HttpMethod method = new HttpMethod();
        method.setKvdata(map);
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("Login");
        HttpClient.postdata(new Button(LoginActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {
                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)){
                    HashMap<String,String> map = result.getKvData();
                    UserInfo info = new UserInfo();
                    if (map != null && map.size() > 0){
                        info.setRealname(map.get("REALNAME").toString());
                        info.setUserid(map.get("USER_ID").toString());
                        info.setUserOrgId(map.get("ORG_ID").toString());
                        info.setUsername(map.get("USERNAME").toString());
                        info.setUserMobile(map.get("MOBILE").toString());
                        info.setSex(map.get("SEX").toString());
                        info.setUserOrgName(map.get("ORG_NAME_CN").toString());
                        info.setUserOrgtype(map.get("ORG_TYPE").toString());
                        info.setToken(map.get("TOKEN").toString());


                        editor.putString(ParamCommon.APP_IS_AUTO_LOGIN,"1");
                        editor.putString(ParamCommon.APP_LOGIN_NAME,loginname);
                        editor.putString(ParamCommon.APP_LOGIN_PASSWORD,password);
                        editor.commit();

                        Looper mainLooper = Looper.getMainLooper();
                        MyHandler handler = new MyHandler(mainLooper);
                        Message msg = new Message();
                        msg.what = 0;
                        handler.sendMessage(msg);

                        myApplication.setUser(info);

                    }

                }else {
                    showToast(result.getResultMessage());
                }

            }
        });
//        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("USERNAME", "11111");
//        map.put("PASSWORD", "11111");
//
//        HttpMethod method = new HttpMethod();
//
//        method.setKvdata(map);
//        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
//        method.setClassPath("com.mobile.Execute");
//        method.setMethodName("Login");
//        HttpResult result = HttpClient.postdata(new Button(LoginActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
////            @Override
////            public void run(HttpResult result) {
////                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)){
////                    HashMap<String,String> map = result.getKvData();
////                    UserInfo info = new UserInfo();
////                    if (map != null && map.size() > 0){
////                        info.setRealname(map.get("REALNAME").toString());
////                        info.setUserid(map.get("USER_ID").toString());
////                        info.setUserOrgId(map.get("ORG_ID").toString());
////                        info.setUsername(map.get("USERNAME").toString());
////                        info.setUserMobile(map.get("MOBILE").toString());
////                        info.setSex(map.get("SEX").toString());
////                        info.setUserOrgName(map.get("ORG_NAME_CN").toString());
////                        info.setUserOrgtype(map.get("ORG_TYPE").toString());
////
////                        editor.putString(ParamCommon.APP_IS_AUTO_LOGIN,"1");
////                        editor.putString(ParamCommon.APP_LOGIN_NAME,loginname);
////                        editor.putString(ParamCommon.APP_LOGIN_PASSWORD,password);
////                        editor.commit();
////
////                        Looper mainLooper = Looper.getMainLooper();
////                        MyHandler handler = new MyHandler(mainLooper);
////                        Message msg = new Message();
////                        msg.what = 0;
////                        handler.sendMessage(msg);
////
////                        myApplication.setUser(info);
////
////                    }
////
////                }else {
////                    showToast(result.getResultMessage());
////                }
////            }
//            });
//        System.out.println(result.getResultCode());
//        System.out.println(result.getResultMessage());
    }


    private class MyHandler extends Handler
    {
        public MyHandler(Looper looper)
        {
            super(looper);
        }

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 0:
                   Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    break;

                default:
                    break;
            }
        }
    }
}
